package com.sun.lib_tag;
/*
 *
 * #                                                   #
 * #                       _oo0oo_                     #
 * #                      o8888888o                    #
 * #                      88" . "88                    #
 * #                      (| -_- |)                    #
 * #                      0\  =  /0                    #
 * #                    ___/`---'\___                  #
 * #                  .' \\|     |# '.                 #
 * #                 / \\|||  :  |||# \                #
 * #                / _||||| -:- |||||- \              #
 * #               |   | \\\  -  #/ |   |              #
 * #               | \_|  ''\---/''  |_/ |             #
 * #               \  .-\__  '-'  ___/-. /             #
 * #             ___'. .'  /--.--\  `. .'___           #
 * #          ."" '<  `.___\_<|>_/___.' >' "".         #
 * #         | | :  `- \`.;`\ _ /`;.`/ - ` : | |       #
 * #         \  \ `_.   \_ __\ /__ _/   .-` /  /       #
 * #     =====`-.____`.___ \_____/___.-`___.-'=====    #
 * #                       `=---='                     #
 * #     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   #
 * #                                                   #
 * #               佛祖保佑         永无BUG              #
 * #                                                   #
 */

import android.view.View;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * Created by ESSC07 on 2021/12/14.
 */
public abstract class TagAdapter<T> {
    private List<T> tagDatas;
    private OnDataChangedListener onDataChangedListener;
    private HashSet<Integer> checkedList = new HashSet<>();

    HashSet<Integer> getCheckedList() {
        return checkedList;
    }

    void setOnDataChangedListener(OnDataChangedListener listener) {
        this.onDataChangedListener = listener;
    }

    public TagAdapter() {
        this.tagDatas = new ArrayList<>();
    }

    public List<T> getDataList() {
        return this.tagDatas;
    }

    public int getItemCount() {
        return tagDatas.size();
    }

    public T getItem(int position) {
        if (position > -1 && position < getItemCount()) {
            return tagDatas.get(position);
        }
        return null;
    }

    public abstract View getView(TagFlowGroup parent, int position, T t);

    public void setTagList(List<T> list) {
        this.tagDatas.clear();
        if (list != null)
            this.tagDatas.addAll(list);
        notifyDataChanged();
    }

    public void setTags(T... tags) {
        this.tagDatas.clear();
        if (tags != null && tags.length > 0) {
            for (T t : tags)
                this.tagDatas.add(t);
        }
        notifyDataChanged();
    }

    public void addTagList(List<T> list) {
        if (list != null)
            this.tagDatas.addAll(list);
        notifyDataChanged();
    }

    public void onSelected(int position, View view) {
        checkedList.add(position);
    }

    public void onUnSelected(int position, View view) {
        if (checkedList.contains(position))
            checkedList.remove(position);
    }

    public void singleSelect(int position){
        checkedList.clear();
        checkedList.add(position);
    }

    public void clearAllSelect(){
        checkedList.clear();
    }

    public boolean isSelected(int position, T t) {
        return checkedList.contains(position);
    }


    public void notifyDataChanged() {
        if (onDataChangedListener != null) onDataChangedListener.onDataChanged();
    }

    interface OnDataChangedListener {
        void onDataChanged();
    }
}
